package org.wuhong.controller;

import com.auth0.jwt.JWT;
import com.wuhong.entity.Accounts;
import com.wuhong.entity.Files;
import com.wuhong.result.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wuhong.service.FilesService;
import org.wuhong.utils.JWTUtil;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**

 * @Description TODO
 * @Author director吴
 * @Date 2022/2/10 11:39
 * @Version 1.0
 **/
@RequestMapping("/file")
@ResponseBody
@Controller
public class FilesController {

    @Autowired
    FilesService fileDownService;

    /**
     * @param file 传参文件
     * @return 返回上传文件的路径的回复信息
     * @throws IOException
     */
    @RequestMapping("/upload")
    public ResultData uploadFileByAnything(@RequestParam("file") MultipartFile file, HttpServletRequest request,@RequestParam("folderId") String folderId) throws Exception {
        String token = request.getHeader("token");
        Accounts account = JWTUtil.getTokenInfo(token);
        return fileDownService.saveFileIo(file,account,folderId);
    }


    @PostMapping("/getFileListByUserId")
    public ResultData<List<Files>> getFileListByUserId(@RequestBody Map<String,Object> map) throws Exception {
        return  fileDownService.getFileListByUserId((String) map.get("userId"));
    }

    @PostMapping("/getFilesInfo")
    public ResultData<List> getFilesInfo(@RequestBody Map<String,Object> map) throws Exception {
        return  fileDownService.getFilesInfo((String) map.get("userId"));
    }
}
